package com.carey.gae.data;

public class PropertiesManager {
	private static final OAuthMemcache mm = OAuthMemcache.getInstance();

	public static void setProperty(String consumer_key, OAuthProperties value) {
		OAuthProperties op = mm.getProperty(consumer_key);
		if (op == null || !op.equals(value)) {
			mm.setProperty(consumer_key, value);
			if (value.getAuthorize_token() != null) {
				mm.setProperty(value.getAuthorize_token(), value);
			}
			OAuthDatabase.setProperty(consumer_key, value);
		}
	}

	public static OAuthProperties getProperty(String consumer_key) {
		if (mm.containsKey(consumer_key)) {
			return mm.getProperty(consumer_key);
		} else {
			OAuthProperties op = OAuthDatabase.getProperty(consumer_key);
			if (op != null) {
				mm.setProperty(consumer_key, op);
				if (op.getAuthorize_token() != null) {
					mm.setProperty(op.getAuthorize_token(), op);
				}
			}
			return op;
		}
	}

	public static OAuthProperties getPropertyByAuthorizeToken(
			String authorize_token) {
		if (mm.containsKey(authorize_token)) {
			return mm.getProperty(authorize_token);
		} else {
			OAuthProperties op = OAuthDatabase
					.getPropertyByAuthorizeToken(authorize_token);
			if (op != null) {
				mm.setProperty(authorize_token, op);
				mm.setProperty(op.getConsumer_key(), op);
			}
			return op;
		}
	}
}
